
package com.wwy.monitor.service.impl;

import com.wwy.monitor.common.MessageException;
import com.wwy.monitor.mapper.tddl.MonitorConfigDetailMapper;
import com.wwy.monitor.mapper.tddl.MonitorReceiverMapper;
import com.wwy.monitor.model.enumeration.EnableStatusEnum;
import com.wwy.monitor.model.tddl.dto.MonitorConfigDetailDTO;
import com.wwy.monitor.model.tddl.dto.MonitorReceiverDTO;
import com.wwy.monitor.model.tddl.query.MonitorConfigDetailQuery;
import com.wwy.monitor.model.tddl.vo.MonitorConfigDetailVo;
import com.wwy.monitor.service.MonitorConfigDetailService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tianquan.ld
 * @version V1.0
 * <p>Description: çæ§配置详情 service impl</p>
 * @date 2019-10-30 21:18
 */
@Service(value = "monitorConfigDetailService")
public class MonitorConfigDetailServiceImpl implements MonitorConfigDetailService {
    @Resource
    private MonitorConfigDetailMapper monitorConfigDetailMapper;
    @Autowired
    private MonitorReceiverMapper monitorReceiverMapper;

    @Override
    public boolean insert(MonitorConfigDetailDTO entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
//        Integer appStatus = monitorConfigDetailMapper.selectAppStatusById(entity.getId());
//        if (null == appStatus || EnableStatusEnum.DISABLE.getValue() == appStatus) {
//            throw new MessageException("åºç¨ä¸å¯ç¨ï¼");
//        }
        return monitorConfigDetailMapper.insert(entity) == 1;
    }

    @Override
    public boolean deleteById(Long id) {
        return monitorConfigDetailMapper.deleteById(id) == 1;
    }

    @Override
    public int deleteByAppId(Long appId) {
        return monitorConfigDetailMapper.deleteByAppId(appId);
    }

    @Override
    public boolean updateById(MonitorConfigDetailDTO entity) {
        entity.setUpdateTime(new Date());
        if (entity.getStatus() != null && EnableStatusEnum.ENABLE.getValue() == entity.getStatus()) {
            Integer appStatus = monitorConfigDetailMapper.selectAppStatusById(entity.getId());
            if (null == appStatus || EnableStatusEnum.DISABLE.getValue() == appStatus) {
                throw new MessageException("åºç¨ä¸å¯ç¨ï¼");
            }
        }
        return monitorConfigDetailMapper.updateById(entity) == 1;
    }

    @Override
    public int offlineConfigDetailByAppId(Long appId) {
        return monitorConfigDetailMapper.offlineConfigDetailByAppId(appId);
    }

    @Override
    public List<MonitorConfigDetailVo> selectByQuery(MonitorConfigDetailQuery query) {
        List<MonitorConfigDetailVo> details = monitorConfigDetailMapper.selectByQuery(query);
//è·åæ¯ä¸ªéç½®è¯¦æä¸çæ¥è­¦äººæ¥åäººåè¡¨
        List<Long> detailIds = details.stream().map(MonitorConfigDetailVo::getId).collect(Collectors.toList());
        List<MonitorReceiverDTO> receiverDTOS = monitorReceiverMapper.selectByDetailIds(detailIds);
//æ ¹æ®detailIdåºåæ¥è­¦äººæ¥åäºº
        Map<Long, List<MonitorReceiverDTO>> temp = Maps.newHashMap();
        for (MonitorReceiverDTO dto : receiverDTOS) {
            if (temp.containsKey(dto.getDetailId())) {
                List<MonitorReceiverDTO> list = temp.get(dto.getDetailId());
                list.add(dto);
                temp.put(dto.getDetailId(), list);
            } else {
                List<MonitorReceiverDTO> list = Lists.newArrayList();
                list.add(dto);
                temp.put(dto.getDetailId(), list);
            }
        }
//å°æ¥è­¦äººæ¥æ¶äººéåå°è£è¿detailVO å¹¶ èµå¼å»¶è¿æ¥è­¦å¼å¯ç¶æ
        Date nowTime = new Date();
        for (MonitorConfigDetailVo detailVo : details) {
            detailVo.setReceiverList(temp.get(detailVo.getId()));
            if (null != detailVo.getAlertDelayTime() && detailVo.getAlertDelayTime().getTime() - nowTime.getTime() > 2000) {
                detailVo.setDelayStatus(0);
            }
        }
        return details;
    }

    @Override
    public List<MonitorConfigDetailDTO> selectByCondition(MonitorConfigDetailQuery query) {
        return monitorConfigDetailMapper.selectByCondition(query);
    }

    @Override
    public List<MonitorConfigDetailVo> selectByAppIdAndIds(Long appId, List<Long> list) {
        return monitorConfigDetailMapper.selectByAppIdAndIds(appId, list);
    }

    @Override
    public MonitorConfigDetailDTO selectById(Long id) {
        return monitorConfigDetailMapper.selectById(id);
    }

    @Override
    public MonitorConfigDetailVo findById(Long detailId) {
        return monitorConfigDetailMapper.findById(detailId);
    }

    @Override
    public Integer count(MonitorConfigDetailQuery query) {
        return monitorConfigDetailMapper.count(query);
    }

    @Override
    public Integer selectCountByDetailName(Long id, Long appId, String detailName) {
        return monitorConfigDetailMapper.selectCountByDetailName(id, appId, detailName);
    }

    @Override
    public boolean updateAlertDelayTimeById(MonitorConfigDetailDTO entity) {
        return monitorConfigDetailMapper.updateDelayTimeById(entity) > 0;
    }
}